// CVis.cpp
// Developed by Yang Yu (yuy@janelia.hhmi.org), Sept 24, 2012
// 

#include "CVis.h"


// Version Control
void CVisVersion()
{
    cout<<"CVis version 1.0.0 "<<" 20120925 (experimental) (cvis)"<<endl;
}

// Help Info
void CVisHelp()
{
    cout<<" CVis: C for Vision and Visualization"<<endl;
    cout<<" CVis Usage: "<<endl;
    
    cout<<" -i <files> input files"<<endl;
    cout<<" -o <files> output files"<<endl;
    
    cout<<" -exec <function> function"<<endl;
    cout<<" -p <parameters> a single string of paramters"<<endl;
    
    cout<<"\nExample: cvis -exec registration -i moving.nii.gz fixed.nii.gz -o deformed.nii.gz"<<endl;
    
    return;
}

// Main Function
int main(int argc, char **argv)
{

    //
    CVisCommandLineParser commands;
    commands.parse(argc, argv, CVisHelp, CVisVersion);
    
    // TEST: read and write
    
    char *input=NULL, *output=NULL;
    if(commands.cli.inputsList.size()!=0)
    {
        input = commands.cli.inputsList.at(0);
    }
    if(commands.cli.outputsList.size()!=0)
    {
        output = commands.cli.outputsList.at(0);
    }
    
    char *paras=NULL;
    if(commands.cli.parametersList.size()!=0)
    {
        paras = commands.cli.parametersList.at(0);
    }
    char *func=NULL;
    if(commands.cli.funcname!=NULL)
    {
        func = commands.cli.funcname;
    }
    
    char tiffext[] = ".tif";
    char niftiext[] = ".nii";
    char niftigzext[] = ".nii.gz";
    
    void *pData = NULL;
    LONG64 sx, sy, sz, sc;
    int datatype;

    //
    if(input)
    {
        CVisImageIO imageIO;
        
        if( check_fileformat(input, tiffext) )
        {
            imageIO.setFileFormat(TIFFIMG);
        }
        else if ( check_fileformat(input, niftiext) || check_fileformat(input, niftigzext))
        {
            imageIO.setFileFormat(NIFTIIMG);
        }
        else
        {
            cout<<"Unsupported input"<<endl;
            return -2;
        }
        
        if(imageIO.read(input))
        {
            sx = imageIO.getDimx();
            sy = imageIO.getDimy();
            sz = imageIO.getDimz();
            sc = imageIO.getDimc();
            
            datatype = imageIO.getDataType();
        }
        else
        {
            cout<<"Unable to read file"<<endl;
            return -2;
        }
        
        if(output)
        {
            // 
            if(!strcmp(func, "testimageio"))
            {
                imageIO.write(output);
            }
            else if(func)
            {
                cout<<"Unsupported function"<<func<<endl;
                return -2;
            }
        }
    }
    
    return 0;
}
